/**
 * Created by mhy on 2018/6/30.
 */
import * as API from '../../api'
import * as types from '../mutation-types'

const state = {
  state: {
    factoryList: {
        list: [],
        page: 1,
        total: 0
    },
    factoryOptionsSelect: [],
    factoryOptionsData: [],
    factoryData: {}
  },
  mutations: {
    [types.GET_FACTORY_LIST_SUCCESS] (state, payload) {
        state.factoryList = payload

    },
    [types.GET_ALL_FACTORY_LIST] (state, payload) {
        let list = payload.list
        state.factoryOptionsSelect = []
        state.factoryOptionsData = []
        if (list.length > 0){
          state.factoryOptionsSelect.push({value: '0',label: '全部',factoryid: '',factoryname: ''})
         for(var i = 0;i < list.length; i ++){
            let item = list[i]
           state.factoryOptionsSelect.push({value: String(i + 1), label:item.name,factoryid: item.id,factoryname:item.name})
           state.factoryOptionsData.push({value: String(i + 1), label:item.name,factoryid: item.id,factoryname:item.name})
         }
        }
    },
    [types.GET_FACTORY_DATA_SUCCESS] (state, payload) {
        state.factoryData = payload
    }
  },
  getters: {

  },
  actions: {
    // 上传图片
    uploadFactoryImage ({commit,state,rootState}, formData) {
      var prom = API.uploadFactoryImage(formData)
      prom.then(function (data) {
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    // 上传图片
    addNewFactory ({commit,state,rootState}, params) {
        var prom = API.addNewFactory(params)
        prom.then(function (data) {
            rootState.reqError = null
        })
        .catch(function (err) {
        rootState.reqError = err
        })
        .finally(function () {
        rootState.reqShake++
        })
        return prom
    },
    getFactoryList ({commit,state,rootState}, {page, search}) {
        var prom = API.getFactoryList({page, search})
        prom.then(function (data) {
            commit(types.GET_FACTORY_LIST_SUCCESS, data.result)
            rootState.reqError = null
        })
        .catch(function (err) {
        rootState.reqError = err
        })
        .finally(function () {
        rootState.reqShake++
        })
        return prom
    },
    getALLFactoryList ({commit,state,rootState}, {page, id}) {
      var prom = API.getFactoryList({page, id})
      prom.then(function (data) {
        commit(types.GET_ALL_FACTORY_LIST, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    updateFactoryData ({commit,state,rootState}, params) {
        var prom = API.updateFactoryData(params)
        prom.then(function (data) {
            rootState.reqError = null
        })
        .catch(function (err) {
        rootState.reqError = err
        })
        .finally(function () {
        rootState.reqShake++
        })
        return prom
    },
    getFactoryData ({commit,state,rootState}, id) {
        var prom = API.getFactoryData(id)
        prom.then(function (data) {
            commit(types.GET_FACTORY_DATA_SUCCESS, data.result)
            rootState.reqError = null
        })
        .catch(function (err) {
        rootState.reqError = err
        })
        .finally(function () {
        rootState.reqShake++
        })
        return prom
    }
  }
}

export default state
